#!/bin/bash
#
# USAGE:
#	? commandName
#
# DESCRIPTION:
#  ? is a builtin ADORE command.  
#  It displays the help for given adore command.   
#
#
# INPUT:
#  commandName: ADORE command name to get help. A list of commands 
#  can be found at the end of this help message.
#  
# OUTPUT:
#  Help message for the specified command, or this message if no command is given.
# 
# 
# ******ADORE Commands******

#RUN: typeset -F | cut -d" " -f3 | grep -v '^_' |grep -v 'command_not_found_handle'| column
# typeset -F : show function names
# cut -d" " -f3 : get field 3, which has the function name
# grep -v '^_' : remove completion functions starting with _
# grep -v 'command_not_found_handle': remove the command not found command...
# column: columnize the output. 

#  [ "${1:-undefined}" == "undefined" ] && echo "..${1}.."
  if [ "${1:-undefined}" == "undefined" ]; then
    sed -n -e '/^# USAGE:/,/^$/ s/^# \?//p' < "${ADORESCR}/fun/h"
    typeset -F | cut -d" " -f3 | grep -v '^_' |grep -v 'command_not_found_handle'| column
    echo ""
    echo "HTML version of the ADORE help can be accessed by browsing to:"
    echo "file:///${ADOREFOLDER}/man/html/links.html"
  elif [ -e "${ADOREFOLDER}/man/txt/${1}" ]; then
    cat "${ADOREFOLDER}/man/txt/${1}"
  elif [ -e "${ADORESCR}/${1}" ]; then 
    sed -n -e '/^# USAGE:/,/^$/ s/^# \?//p' < ${ADORESCR}/${1}
  elif [ -e "${ADORESCR}/process/${1}" ]; then 
    sed -n -e '/^# USAGE:/,/^$/ s/^# \?//p' < ${ADORESCR}/process/${1}              
  elif [ -e "${ADORESCR}/fun/${1}" ]; then 
    sed -n -e '/^# USAGE:/,/^$/ s/^# \?//p' < ${ADORESCR}/fun/${1}
  elif [ -e "${1}" ]; then 
    sed -n -e '/^# USAGE:/,/^$/ s/^# \?//p' < ${1}
  else
    echo "Unknown ADORE command: ${1}"
    echo 'You can get a list of ADORE commands with the command: ?'
  fi
